<template>
  <div>
    <a-input-group class="group">
      <div class="item">
        <span class="title">要找回的账号：</span
        ><a-input
          v-model="account"
          class="input"
          placeholder="请输入账号"
        />
        <a-button type="primary" :loading="loading" @click="sendEmail()">{{
          sendBtnText
        }}</a-button>
      </div>
      <div class="item">
        <span class="title">验证码：</span
        ><a-input
          v-model="code"
          class="input"
          placeholder="请输入邮箱验证码"
        />
      </div>
      <div class="item">
        <span class="title">设置密码：</span
        ><a-input-password
          v-model="password"
          class="input"
          placeholder="请设置新密码"
        />
      </div>
    
    </a-input-group>
    <div class="sumbit" @click="toSumbit()">重新设置密码</div>
  </div>
</template>
  

<script lang="ts">
export default {
  name: "ForgetItem",
  components: {},
  data() {
    return {
      title: "忘记密码",
      account: "",
      password: "",
      code:"",
      loading: false,
      sendBtnText: "发送验证码",
      reg: /^([a-zA-Z]|[0-9])(\w|-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/,
    };
  },

  created() {},
  mounted() {

  },
  methods: {
    toSumbit() {
      if (this.account == "" || this.account == null) {
        this.$message.error("请输入要找回密码的账号");
        return;
      }

      if (!this.reg.test(this.account)) {
        this.$message.error("账号必须是邮箱格式");
        return;
      }

      if (this.password == "" || this.password == null) {
        this.$message.error("请输入新的密码");
        return;
      }
  
      this.toPostResetPwd();
    },
    toPostResetPwd() {
      let param = {
        username: this.account,
        password: this.password,
        code:this.code,
      };
      let self = this;

      this.$api.login.postResetPwd(param).then((res) => {
        console.log(res);
        if (res.code == 1) {
          // console.log(res.code);
          window.localStorage.clear();
          // window.localStorage.setItem('userid',JSON.stringify(res.data.userInfo.id))
          // window.localStorage.token = res.data.token;
          // console.log("token=" + window.localStorage["token"]);

          self.$parent.tabPosition =0;
          // this.$store.commit('setToken',JSON.stringify(res.data.token))
        }
        // this.data = res.data.data;
        // this.loading = false;
      });
    },
    sendEmail() {
      if (this.account == "" || this.account == null) {
        this.$message.error("请输入要找回的密码的账号");
        return;
      }

      if (!this.reg.test(this.account)) {
        this.$message.error("账号必须是邮箱格式");
        return;
      }

      this.loading = true;
      this.sendBtnText = "发送中...";
      let param = {
        username: this.account,
      };
      // let self = this;

      this.$api.login.sendForgetEmail(param).then((res) => {
        console.log(res);
        this.loading = false;
        this.sendBtnText = "重新发送";
      });
    },
  },
};
</script>

<style scoped lang="scss">
.group {
  padding-top: 30px;
  padding-bottom: 10px;
  .item {
    display: flex;
    flex-direction: row;
    height: 50px;
    line-height: 50px;
    align-items: center;
    .title {
      text-align: left;
      font-size: 16px;
      color: black;
      width: 160px;
      padding-left: 30px;
    }
    .input {
      margin-right: 30px;
      width: 180px;
      text-align: left;
    }
  }
}

.sumbit {
  background: #1890ff;
  border-radius: 8px;
  height: 44px;
  color: whitesmoke;
  font-weight: 800;
  text-align: center;
  line-height: 44px;
  margin-left: 40px;
  margin-right: 40px;
  font-size: 20px;
}
</style>
